package com.sinosoft.dsp.platform.services;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;

import com.sinosoft.builder.templates.app.AppSetup;
import com.sinosoft.builder.templates.app.ApplicationType;
import com.sinosoft.builder.templates.app.CommandType;
import com.sinosoft.builder.templates.app.MenuItem;
import com.sinosoft.builder.templates.app.NavigationType;
import com.sinosoft.builder.templates.app.Parameter;
import com.sinosoft.dsp.platform.manager.ApplicationBuilder;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 平台导航服务类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-4-15 下午12:11:50
 */
@ManagedBean(name = "platformNavigationService")
@NoneScoped
public class PlatformNavigationService {

	private static final Log log = Log.getLog(PlatformNavigationService.class
			.getName());

	/**
	 * 通过ID获得应用对象
	 * 
	 * @param id
	 * @return
	 */
	public ApplicationType getApplicationById(String id) {
		try {
			ApplicationBuilder ab = SinoManagedBean
					.findBean("applicationBuilder");
			if (ab.getApplications() == null) {
				Map<String, ApplicationType> applications = new HashMap<String, ApplicationType>();
				for (AppSetup app : ab.getApps()) {
					for (ApplicationType application : app.getApplication()) {
						if (!Const.isEmpty(application.getId())) {
							if (applications.containsKey(application.getId())) {
								log.error("初始化应用时，发现重复的ID:"
										+ application.getId());
							}
							applications.put(application.getId(), application);
						}
					}
				}
				ab.setApplications(applications);
			}
			return ab.getApplications().get(id);
		} catch (Exception e) {
			log.error("通过ID获得应用对象出现错误(getApplicationById)！");
		}
		return null;
	}

	/**
	 * 通过ID获得导航
	 * 
	 * @param id
	 * @return
	 */
	public NavigationType getNavigationById(String id) {
		try {
			ApplicationBuilder ab = SinoManagedBean
					.findBean("applicationBuilder");
			if (ab.getNavigations() == null) {
				Map<String, NavigationType> navigations = new HashMap<String, NavigationType>();
				for (AppSetup app : ab.getApps()) {
					for (ApplicationType application : app.getApplication()) {
						for (NavigationType n : application.getNavigation()) {
							if (!Const.isEmpty(n.getId())) {
								if (navigations.containsKey(n.getId())) {
									log.warn("初始化导航时，发现重复的ID，原有导航被替代。ID:"
											+ n.getId());
								}
								navigations.put(n.getId(), n);
							}
						}
					}
				}
				ab.setNavigations(navigations);
			}
			return ab.getNavigations().get(id);
		} catch (Exception e) {
			log.error("通过ID获得导航出现错误(getNavigationById)！");
		}
		return null;
	}

	/**
	 * 通过ID获得菜单项
	 * 
	 * @param id
	 * @return
	 */
	public MenuItem getMenuItemById(String id) {
		try {
			ApplicationBuilder ab = SinoManagedBean
					.findBean("applicationBuilder");
			if (ab.getNaviItems() == null) {
				Map<String, MenuItem> naviItems = new HashMap<String, MenuItem>();
				initMenuItem(naviItems);
				ab.setNaviItems(naviItems);
			}
			return ab.getNaviItems().get(id);
		} catch (Exception e) {
			log.error("通过ID获得菜单项出现错误(getMenuItemById)！");
		}
		return null;
	}

	/**
	 * 获得应用ID获得应用的所有菜单
	 * 
	 * @param applicationId
	 * @return
	 */
	public Map<String, MenuItem> getNaviItems(String applicationId) {
		try {
			ApplicationType application = getApplicationById(applicationId);
			Map<String, MenuItem> m = new HashMap<String, MenuItem>();
			for (NavigationType n : application.getNavigation()) {
				List<MenuItem> miList = n.getMenuItem();
				for (MenuItem mi : miList) {
					initSubMenuItem(m, mi);
				}
			}
			return m;
		} catch (Exception e) {
			log.error("获得应用ID获得应用的所有菜单出现错误(getNaviItems)！");
		}
		return null;
	}

	private void initMenuItem(Map<String, MenuItem> naviItems) {
		ApplicationBuilder ab = SinoManagedBean.findBean("applicationBuilder");
		for (AppSetup app : ab.getApps()) {
			for (ApplicationType application : app.getApplication()) {
				for (NavigationType n : application.getNavigation()) {
					List<MenuItem> miList = n.getMenuItem();
					for (MenuItem mi : miList) {
						initSubMenuItem(naviItems, mi);
					}
				}
			}
		}
	}

	private void initSubMenuItem(Map<String, MenuItem> naviItems, MenuItem mi) {
		if (mi != null && !Const.isEmpty(mi.getId())) {
			naviItems.put(mi.getId(), mi);
			List<MenuItem> subMiList = mi.getMenuItem();
			if (subMiList != null && subMiList.size() > 0) {
				for (MenuItem subMi : subMiList) {
					initSubMenuItem(naviItems, subMi);
				}
			}
		}
	}

	/**
	 * 通过命令和key获得命令的参数
	 * 
	 * @param c
	 * @param key
	 * @return
	 */
	public Parameter getParameterByCommandType(CommandType c, String key) {
		try {
			if (c != null) {
				Parameter p;
				for (Iterator<Parameter> iter = c.getParameter().iterator(); iter
						.hasNext();) {
					p = iter.next();
					if (p.getKey().equals(key)) {
						return p;
					}
				}
			}
		} catch (Exception e) {
			log.error("通过命令和key获得命令的参数出现错误(getNaviItems)！");
		}
		return null;
	}

}
